package com.jh.jingfei.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jh.jingfei.R;

/**
 * Created by lion on 2017/12/2.
 * @author lxb
 * 自定义toolbar
 */

public class MyToolbar extends Toolbar {

    private TextView mTitleTextView;
    private ImageView mRightView;

    private Drawable mRightIcon;

    private String mTitleText;

    public MyToolbar(Context context) {
        this(context, null);
    }

    public MyToolbar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    private void init() {
        mTitleTextView = findViewById(R.id.toolbar_title);
        mRightView = findViewById(R.id.toolbar_more);
        mRightView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onRightMenuClickListener != null) {
                    onRightMenuClickListener.onClick(v);
                }
            }
        });
    }

    public void setRightIcon(Drawable drawable) {
        mRightIcon = drawable;
        mRightView.setImageDrawable(drawable);
    }

    public void setRightIconResource(@DrawableRes int resId) {
        mRightView.setImageResource(resId);
        mRightIcon = mRightView.getDrawable();
    }

    @Override
    public void setTitle(@StringRes int resId) {
        mTitleTextView.setText(resId);
        mTitleText = getContext().getString(resId);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitleTextView.setText(title);
        mTitleText = (String) title;
    }

    public void setRightMenuGone(boolean gone) {
        if (gone) {
            mRightView.setVisibility(GONE);
        } else {
            mRightView.setVisibility(INVISIBLE);
        }
    }

    public void setRightMenuVisible(boolean visible) {
        if (visible) {
            mRightView.setVisibility(VISIBLE);
        } else {
            mRightView.setVisibility(INVISIBLE);
        }
    }

    private OnRightMenuClickListener onRightMenuClickListener;

    public void setOnRightMenuClickListener(OnRightMenuClickListener onRightMenuClickListener) {
        this.onRightMenuClickListener = onRightMenuClickListener;
    }

    public interface OnRightMenuClickListener {
        void onClick(View v);
    }
}
